

<%@page import="DAL.MyConnection"%>
<%@page import="java.sql.Connection"%>
<%@page import="DAL.StudentDAO"%>
<%@page import="java.util.Arrays"%>
<%@page import="DAL.Student"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body style="background-image: url('http://localhost:8080/ftico_project/pic/hinh-nen-hd-dep-cho-may-tinh-canh-bien-wallpaper-12.jpg')" >
        <div align = "center">
        <h1>Add new student</h1>
        <form method="POST">
            <table border = "1" style="background-color: white" >
            <tr>
            <td>
            <table>
                <tr>
                    <td>Id:</td>
                    <td><input type="text" name="id"></td>
                </tr>
                <tr>
                    <td>Name:</td>
                    <td><input type="text" name="name"></td>
                </tr>
                <tr>
                    <td>Age:</td>
                    <td><input type="text" name="age"></td>
                </tr>
                <tr>
                    <td>Sex:</td>
                    <td><input type="radio" name="sex" value="male">Male
                        <input type="radio" name="sex" value="female">Female</td>
                </tr>
                <tr>
                    <td>Home Town:</td>
                    <td><input type="text" name="hometown"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Submit">
                        <input type="reset" value="Clear"></td>
                </tr>
            </table>
            </td>
            </tr>
            </table>
        </form>
        </div>
        <div align="center" style="font-size: 10px">
            <a HREF ="login.jsp" style="text-decoration:none;" ><h2>Logout</h2></a>
        </div>
        <%

            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String age = request.getParameter("age");
            String sex = request.getParameter("sex");
            String hometown = request.getParameter("hometown");

            if (name != null && age != null && hometown != null && id != null) {
                boolean dataValid = true;
                if(id == ""){
                    dataValid = false;
                    out.println("Id cannot be null!");
                }
                if (name == "" || name.trim().equals(" ")) {
                    dataValid = false;
                    out.println("Name cannot be null or blank!");
                }
                try {
                    Integer.parseInt(age);
                } catch (Exception ex) {
                    dataValid = false;
                    out.println("Age cannot be null & must be number!");
                }
                if (dataValid) {
                    Student st = new Student(id, name, Integer.parseInt(age), sex, hometown);
                    try {
                        Connection con = MyConnection.getConnection();
                        StudentDAO studentDAO = new StudentDAO(con);
                        studentDAO.insertStudent(st);
                        out.println("Insert OK");
                        con.close();
                        response.sendRedirect("list.jsp");
                    } catch (Exception ex) {
                        out.println(ex.getMessage());
                    }
                }
            }
        %>
    </body>
</html>
